package com.haiertvbic.timeline.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.util.Xml;

import com.haiertvbic.timeline.bean.ImageInfo;
import com.haiertvbic.tvnow.util.SLog;

public class TimeLineUtil {
	private static LinkedList<ImageInfo> mLinkedList;

	public static List<?> parseNETXML(String xml) throws Exception {
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(new ByteArrayInputStream(xml.getBytes()), "UTF-8");
		ImageInfo imageInfo = null;
		int event = parser.getEventType();// 产生第一个事件
		while (event != XmlPullParser.END_DOCUMENT) {
			switch (event) {
			case XmlPullParser.START_DOCUMENT:
				mLinkedList = new LinkedList<ImageInfo>();
				break;
			case XmlPullParser.START_TAG:
				if ("img".equals(parser.getName())) {
					imageInfo = new ImageInfo();
				} else if ("id".equals(parser.getName())) {
					imageInfo.setImageId(parser.nextText());
				} else if ("url".equals(parser.getName())) {
					imageInfo.setImageUrl(parser.nextText());
				}
				break;
			case XmlPullParser.END_TAG:
				if ("img".equals(parser.getName())) {
					mLinkedList.add(imageInfo);
				}
				break;
			}
			event = parser.next();
		}
		return mLinkedList;
	}

	public static boolean saveBitmapToFile(Context context, Bitmap bitmap,
			String imageId) {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
		try {
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			FileOutputStream fout = context.openFileOutput(imageId+".png", 0);
			fout.write(out.toByteArray());
			fout.close();
			return true;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	public static boolean deleteAllFile(String path) {
		boolean flag = false;
		File file = new File(path);
		if (!file.exists()) {
			return flag;
		}
		if (!file.isDirectory()) {
			return flag;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			if (path.endsWith(File.separator)) {
				temp = new File(path + tempList[i]);
			} else {
				temp = new File(path + File.separator + tempList[i]);
			}
			if (temp.isFile()) {
				SLog.i("soul", "delete file path = " + path + "/" + tempList[i]);
				temp.delete();
			}
			if (temp.isDirectory()) {
				deleteAllFile(path + "/" + tempList[i]);
				flag = true;
			}
		}
		return flag;
	}
	
	public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		bmp.compress(CompressFormat.PNG, 100, output);
		if (needRecycle) {
			bmp.recycle();
		}
		
		byte[] result = output.toByteArray();
		try {
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
}
